import axios from "axios";
import { baseURL, TIMEOUT } from "./config"
import useMainStore from "@/store/modules/main";
const mainStore = useMainStore()

// 创建类
class rawRequest {
  constructor(baseURL, timeout) {
    this.instance = axios.create(
      {
        baseURL,
        timeout
      }
    )

    this.instance.interceptors.request.use(config => {
      mainStore.isloading = true
      return config
    }, err => {
      return err
    })

    this.instance.interceptors.response.use(res => {
      mainStore.isloading = false
      return res
    }, err => {
      mainStore.isloading = false
      return err
    })
  }

  request(config) {
    return new Promise((resolve, reject) => {
      this.instance.request(config).then(res => {
        // 数据剥离
        resolve(res.data)
      }).catch(err => {
        reject(err)
      })
    })
  }

  get(config) {
    return this.request({ ...config, method: "get" })
  }

  post(config) {
    return this.request({ ...config, method: "post" })
  }
}

export default new rawRequest(baseURL, TIMEOUT)